#####
## Replication File:
##
## "Using an AI-powered ‘Street Epistemologist’ Chatbot 
## and Reflection Tasks to Diminish Conspiracy Theory Beliefs"
##
## Published in HKS Misinformation Review
##
## Marco Meyer, Adam Enders, Casey Klofstad, Justin Stoler and Joseph Uscinski
#####

################################################################################

####
## Load necessary packages
## 
## Analyses conducted in RStudio 1.0.153,
## using R 3.6 (Mac OS)
####

library(lattice)
library(psych)
library(MASS)
library(foreign)
library(gridExtra)
library(ggplot2)

# Set working directory!

################################################################################

####
## Figure 1
####

effects <- read.csv("Effects.csv")

png("figure1.png", width = 5.75, height = 4.5, units = "in", res=300)
dotplot(var ~ estimate,
        data = effects,
        aspect = 1.5,
        xlab = "Change in Belief (Pre-Post)",
        ylab = "",
        scales = list(y = list(labels = effects$label)),
        xlim = c(-1.1, 0.08),
        par.settings = list(clip = list(panel = FALSE)), 
        panel = function(x, y, subscripts, ...){
          panel.dotplot(x, y, col="black", lty = 3, pch = 16)
          panel.abline(v = 0, col = "dark grey")
          panel.segments(effects$lower, effects$var,
                         effects$upper, effects$var, col = "black")
          panel.text(x=0.3, y, 
                     label = format(round(effects$cohen, digits=2), nsmall=2), 
                     cex=0.75)
          panel.text(x=0.3, y=4.5, label = "Cohen's d", cex=0.75)
        }
)
dev.off()

################################################################################

####
## Figure 2
####

effects2 <- read.csv("MFX, Importance.csv")

png("figure2.png", width = 8, height = 6, units = "in", res=300)
xyplot(effect ~ level | as.factor(condition),
       data = effects2,
       aspect = 0.75,
       ylim = c(-2, 1.5),
       xlab = "Level of Importance ('Not At all' to 'Extremely')",
       ylab = "Marginal Effect (Pre-Post Difference)",
       index.cond=list(c(1,4,2,3)),
       par.strip.text=list(cex=0.85),
       panel = function(x, y, subscripts, ...){
         panel.abline(h=0, col = "gray")
         panel.xyplot(x, y, type = "p", col = "black", pch = 16)
         panel.segments(effects2$level, effects2$lower[subscripts], 
                        effects2$level, effects2$upper[subscripts], col = "black")
         #panel.xyplot(effects2$level[subscripts], effects2$lower[subscripts], 
         #            type = "l", col = "black", lty = 2)
         #panel.xyplot(effects2$level[subscripts], effects2$upper[subscripts], 
         #            type = "l", col = "black", lty = 2)
       }
)
dev.off()

################################################################################

####
## Figure 3
####

effects3 <- read.csv("MFX, ACTS.csv")

png("figure3.png", width = 8, height = 6, units = "in", res=300)
xyplot(effect ~ level | condition,
       data = effects3,
       aspect = 0.75,
       xlab = "Level of Conspiracy Thinking",
       ylab = "Marginal Effect (Pre-Post Difference)",
       ylim = c(-1.7, 0.5),
       scales = list(x = list(labels = c("Low", "Middling", "High"), at=c(1, 2, 3))),
       index.cond=list(c(1,4,2,3)),
       par.strip.text=list(cex=0.85),
       panel = function(x, y, subscripts, ...){
         panel.abline(h = 0, col = "dark grey")
         panel.xyplot(x, y, col="black", lty = 3, pch = 16)
         panel.segments(effects3$level, effects3$lower[subscripts], 
                        effects3$level, effects3$upper[subscripts], col = "black")
       }
)
dev.off()

################################################################################

####
## Figure A1
####

mfx <- read.csv("MFX.csv")

png("figureA1.png", width = 5, height = 4, units = "in", res=300)
dotplot(var ~ estimate,
        data = mfx,
        aspect = 1.5,
        xlab = "Marginal Effect (Pre-Post)",
        ylab = "",
        scales = list(y = list(labels = effects$label)),
        xlim = c(-1.1, 0.08),
        par.settings = list(clip = list(panel = FALSE)), 
        panel = function(x, y, subscripts, ...){
                panel.dotplot(x, y, col="black", lty = 3, pch = 16)
                panel.abline(v = 0, col = "dark grey")
                panel.segments(effects$lower, effects$var,
                               effects$upper, effects$var, col = "black")
        }
)
dev.off()

################################################################################

####
## Figure A2
####

balance <- read.csv("Balance.csv")

png("figureA2.png", width = 6, height = 6, units = "in", res=300)
dotplot(label ~ estimate | as.factor(var),
       data = balance,
       aspect = 0.75,
       xlab = "",
       ylab = "",
       scales=list(x=list(relation='free', limits=list(c(35,55), c(0.08,0.28),
                                                       c(1.8,5), c(0.38,0.63), c(0,0.3)))),
       panel = function(x, y, subscripts, ...){
               panel.dotplot(x, y, col="black", lty = 3, pch = 16)
               #panel.xyplot(x, y)
               panel.segments(balance$lower[subscripts], as.factor(balance$label)[subscripts], 
                              balance$upper[subscripts], as.factor(balance$label)[subscripts], col = "black")
       }
)
dev.off()

################################################################################

####
## Figure A3
####

effects <- read.csv("Effects, alt.csv")

png("figureA3.png", width = 5.75, height = 4.5, units = "in", res=300)
dotplot(var ~ estimate,
        data = effects,
        aspect = 1.5,
        xlab = "Change in Belief (Pre-Post)",
        ylab = "",
        scales = list(y = list(labels = effects$label)),
        xlim = c(-1.1, 0.08),
        par.settings = list(clip = list(panel = FALSE)), 
        panel = function(x, y, subscripts, ...){
          panel.dotplot(x, y, col="black", lty = 3, pch = 16)
          panel.abline(v = 0, col = "dark grey")
          panel.segments(effects$lower, effects$var,
                         effects$upper, effects$var, col = "black")
          #panel.text(x+0.1, y+0.1, label = round(effects$cohen, digits=2), cex=0.75)
          panel.text(x=0.3, y, 
                     label = format(round(effects$cohen, digits=2), nsmall=2), 
                     cex=0.75)
          panel.text(x=0.3, y=4.5, label = "Cohen's d", cex=0.75)
        }
)
dev.off()

################################################################################

####
## Figure A4
####

effects2 <- read.csv("MFX, Importance, alt.csv")

png("figureA4.png", width = 8, height = 6, units = "in", res=300)
xyplot(effect ~ level | as.factor(condition),
       data = effects2,
       aspect = 0.75,
       ylim = c(-2, 1.5),
       xlab = "Level of Importance ('Not At all' to 'Extremely')",
       ylab = "Marginal Effect (Pre-Post Difference)",
       index.cond=list(c(1,4,2,3)),
       par.strip.text=list(cex=0.85),
       panel = function(x, y, subscripts, ...){
         panel.abline(h=0, col = "gray")
         panel.xyplot(x, y, type = "p", col = "black", pch = 16)
         panel.segments(effects2$level, effects2$lower[subscripts], 
                        effects2$level, effects2$upper[subscripts], col = "black")
         #panel.xyplot(effects2$level[subscripts], effects2$lower[subscripts], 
         #            type = "l", col = "black", lty = 2)
         #panel.xyplot(effects2$level[subscripts], effects2$upper[subscripts], 
         #            type = "l", col = "black", lty = 2)
       }
)
dev.off()

################################################################################

####
## Figure A5
####

effects3 <- read.csv("MFX, ACTS, alt.csv")

png("figureA5.png", width = 8, height = 6, units = "in", res=300)
xyplot(effect ~ level | condition,
       data = effects3,
       aspect = 0.75,
       xlab = "Level of Conspiracy Thinking",
       ylab = "Marginal Effect (Pre-Post Difference)",
       ylim = c(-1.7, 0.5),
       scales = list(x = list(labels = c("Low", "Middling", "High"), at=c(1, 2, 3))),
       index.cond=list(c(1,4,2,3)),
       par.strip.text=list(cex=0.85),
       panel = function(x, y, subscripts, ...){
         panel.abline(h = 0, col = "dark grey")
         panel.xyplot(x, y, col="black", lty = 3, pch = 16)
         panel.segments(effects3$level, effects3$lower[subscripts], 
                        effects3$level, effects3$upper[subscripts], col = "black")
       }
)
dev.off()
